Overview
Decodes token tensors back into human-readable text. Reverses the tokenization process by converting token IDs to their corresponding text representation.Function Signature
Parameters
torch.Tensor
required
Tensor of token IDs to decode. Can be 1D (single sequence) or 2D (batch of sequences). Token IDs are converted from GPU to CPU if needed.
Returns
str
Decoded text string. Special tokens (SOT, EOT) and padding are included in the output. The
</w> BPE markers are converted to spaces.Examples
Basic decoding
Decode batch of tokens
Decode model predictions
Handle padding and special tokens
Remove special tokens
Decode only non-padding tokens
Decoding Process
The decode function:- Converts token IDs to BPE subword strings
- Joins subwords together
- Decodes byte representation to UTF-8 text
- Replaces
</w>markers with spaces - Handles special tokens like
<start_of_text>and<end_of_text>
Token ID Reference
Notes
- The function automatically moves tensors from GPU to CPU for decoding
- Decoded text includes special tokens (
<start_of_text>,<end_of_text>) - Padding tokens (ID: 0) decode to empty strings but may appear as spaces
- BPE word boundaries (
</w>) are converted to spaces in the output - This uses the module-level
SimpleTokenizerinstance - For custom tokenizers, call the
.decode()method on the tokenizer instance directly
Error Handling
See Also
tokenize()- Convert text to tokensget_tokenizer()- Get model-specific tokenizers
